package org.despair2D.ui.core 
{
	import flash.events.Event;
	import org.despair2D.core.EventDispatcherAdvance;
	import org.despair2D.ui.core.Component;
	import org.despair2D.ui.Fusion;
	import org.despair2D.ui.puppet.*;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
final public class FusionComp extends Component
{
	
	
	final ns_despair function createAnimePuppet( sectionName:String, index:int, name:String ) : AnimePuppet
	{
		var puppet:AnimePuppet = AnimePuppet.getAnimePuppet(sectionName);
		return this._addPuppet(puppet, name, index) as AnimePuppet;
	}
	
	
	final ns_despair function createImagePuppet( isCenter:Boolean, index:int, name:String ) : ImagePuppet
	{
		var puppet:ImagePuppet = ImagePuppet.getImagePuppet(isCenter)
		return this._addPuppet(puppet, name, index) as ImagePuppet
	}

	
	final ns_despair function createNineScalePuppet( displayRef:*, width:Number, height:Number, index:int, name:String  ) : NineScalePuppet
	{
		var puppet:NineScalePuppet = NineScalePuppet.getNineScalePuppet(displayRef, width, height);
		return this._addPuppet(puppet, name, index) as NineScalePuppet;
	}
	
	
	final ns_despair function createDisplayObjectContainerPuppet( cacheAsBitmap:Boolean , index:int, name:String ) : DisplayObjectContainerPuppet
	{
		var puppet:DisplayObjectContainerPuppet = DisplayObjectContainerPuppet.getPuppet(cacheAsBitmap);
		return this._addPuppet(puppet, name, index) as DisplayObjectContainerPuppet;
	}
	
	
	final ns_despair function createTextPuppet( text:String, offsetX:Number, offsetY:Number, embedFont:Boolean, index:int, name:String ) : TextPuppet
	{
		var puppet:TextPuppet = TextPuppet.getTextPuppet(text, offsetX, offsetY, embedFont);
		return this._addPuppet(puppet, name, index) as TextPuppet;
	}
	

	final public function getPuppetByName( name:String ) : IPuppet 
	{
		for (var i:int = 0; i < m_numPuppets; i++) 
		{
			if (m_puppetList[i].name == name) 
			{
				return m_puppetList[i];
			}
		}
		return null;
	}
	
	
	final public function getPuppetAt( index:int ) : IPuppet
	{
		return m_puppetList[index]
	}
	
	
	final ns_despair function removeAllPuppets() : void
	{
		while (--m_numPuppets > -1)
		{
			m_puppetList[m_numPuppets].recycle();
		}
		m_puppetList.length = m_numPuppets = 0;
	}
	
	
	final ns_despair function _addPuppet( puppet:*, name:String , index:int ) : *
	{
		m_puppetList[m_numPuppets++]  =  puppet;
		puppet.m_view.name           =  Boolean(name == null) ? 'puppet_' + m_numPuppets : name
		puppet.m_view.m_fusion       =  this
		
		if (index < 0)
		{
			this.addChild(puppet.m_view);
		}
		
		else
		{
			this.addChildAt(puppet.m_view, index);
		}
		
		return puppet;
	}
	
	
	final ns_despair function removePuppet( puppet:IPuppet ) : void
	{
		var index:int = m_puppetList.indexOf(puppet);
		
		m_puppetList[index] = m_puppetList[--m_numPuppets];
		m_puppetList.pop();
	}
	
	
	final ns_despair function get interactiveZ () : Boolean
	{
		var C:FusionComp;
		
		if (this.interactive)
		{
			C = this.parent as FusionComp
			
			while (C)
			{
				if (!C.interactive)
				{
					return false;
				}
				C = C.parent as FusionComp;
			}
			
			return true;
		}
		
		return false;
	}
	
	
	final ns_despair function bubble( event:Event ) : void
	{
		var FC:FusionComp
		
		this.dispatchEvent(event)
		FC = this.parent as FusionComp;
		while (FC)
		{
			FC.dispatchEvent(event)
			FC = FC.parent as FusionComp;
		}	
	}

	
	final override ns_despair function recycle() : void
	{
		super.recycle();
		
		m_proxy = null;
		m_eventDispatcher = null
		this.removeAllPuppets();
		cachedFusionCompList[cachedFusionCompLength++] = this;
	}
	
	
	ns_despair static function getFusionComp( proxy:Fusion ) : FusionComp
	{
		var fc:FusionComp = (cachedFusionCompLength > 0 ? cachedFusionCompLength-- : 0) ? cachedFusionCompList.pop() : new FusionComp();
		fc.m_proxy = proxy;
		fc.m_eventDispatcher = new EventDispatcherAdvance(proxy);
		return fc;
	}
	
	
	
	ns_despair static var cachedFusionCompList:Array = [];
	
	ns_despair static var cachedFusionCompLength:int;
	
	
	ns_despair var m_puppetList:Array = [];
	
	ns_despair var m_numPuppets:int;

	
}

}
	